//
//  TKProgressView.m
//  Router
//
//  Created by teki on 15/10/16.
//  Copyright © 2015年 dc. All rights reserved.
//

#import "TKProgressView.h"

@interface TKProgressView ()

@end


@implementation TKProgressView

- (instancetype)initWithCoder:(NSCoder *)coder
{
    self = [super initWithCoder:coder];
    if (self) {
        [self initialize];
    }
    return self;
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self initialize];
    }
    return self;
}

- (void)initialize {
    self.backgroundColor = [UIColor clearColor];
}




// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
    NSLog(@"rect:%@",NSStringFromCGRect(rect));
    if (rect.size.height < 28) {
        rect.size.height = 28.0f;
    }
    CGContextRef context = UIGraphicsGetCurrentContext();
    [self drawProgressBackground:context inRect:rect];
    if (self.progress > 0) {
        [self drawProgress:context withFrame:rect];
    }
}


- (void)drawProgressBackground:(CGContextRef)context inRect:(CGRect)rect {
    /// 背景颜色
    CGContextSaveGState(context);
    
    UIBezierPath *roundedRect = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(0, (CGRectGetHeight(rect) - self.progressBarHeight.floatValue)/2.0f, rect.size.width, self.progressBarHeight.floatValue) cornerRadius:0];
    CGContextSetFillColorWithColor(context, self.progressBgColor.CGColor);
    [roundedRect fill];
}


- (void)drawProgress:(CGContextRef)context withFrame:(CGRect)frame {
    UIImage *image = [UIImage imageNamed:@"home_adjust"];
    CGFloat imageEnableLength = CGRectGetWidth(frame)- image.size.width;

    CGRect rectToDrawIn = CGRectMake(0, (CGRectGetHeight(frame) - self.progressBarHeight.floatValue)/2.0f,image.size.width + (imageEnableLength*self.progress - image.size.width /2.0f), self.progressBarHeight.floatValue);
    
    UIBezierPath *roundedRect = [UIBezierPath bezierPathWithRoundedRect:rectToDrawIn cornerRadius:0];
    CGContextSetFillColorWithColor(context, self.progressColor.CGColor);
    [roundedRect fill];


    CGRect imageRect = CGRectMake(image.size.width/2.0f + (imageEnableLength*self.progress - image.size.width /2.0f), (frame.size.height - image.size.height)/2.0, image.size.width, image.size.height);
    
    [image drawInRect:imageRect];

}


- (void)drawRightImageInRect:(CGRect)rect
{
    }


- (void)setProgress:(CGFloat)progress
{
    _progress = progress;
    [self setNeedsDisplay];
}


#pragma mark -
#pragma mark   PrivateMethods
- (UIColor *)progressColor
{
    if (!_progressColor)
    {
        _progressColor = [UIColor colorWithHexString:@"#60a658"];
    }
    return _progressColor;
}


- (UIColor *)progressBgColor{
    if (_progressBgColor == nil) {
        _progressBgColor = [UIColor colorWithHexString:@"#dff7dc"];
    }
    return _progressBgColor;
}


- (NSNumber *)progressBarHeight
{
    if (_progressBarHeight == nil)
    {
        _progressBarHeight = @(6);
    }
    return _progressBarHeight;
}
@end
